
import os
import re
import shutil

ziper = 'compressors\\7za.exe'
zip_level = '-mx9'
html_compressor = 'java -jar compressors/htmlcompressor-1.5.3.jar'
css_compressor = 'java -jar compressors/yuicompressor-2.4.7.jar'
js_compressor = 'java -jar compressors/closure-compiler.jar --language_in=ECMASCRIPT5'
output_css_mark = ' -o '
output_js_mark = ' --js_output_file '
compressPath = 'build/'

crx_builder = '%LOCALAPPDATA%\\Google\\Chrome\\Application\\chrome.exe'
current_path = os.getcwd()
crx_cmdlines = ' --pack-extension="' + current_path + '\\build" --pack-extension-key="' + current_path + '\\build.pem' + '" --no-message-box'

css_to_compress = [ 'baidu/du.css', 'tieba/css/js.css', 'css-set/bdb-common.css', 'css-set/day.css', 'css-set/night.css', 'css-set/blue-white.css', 'bg-page/option-page/css/patch.css', 'bg-page/option-page/css/chrome_shared.css', 'bg-page/option-page/css/button.css', 'bg-page/option-page/css/checkbox.css', 'bg-page/option-page/css/select.css' ]
js_to_compress = [ 'libs/Config.js', 'libs/cQuery.js', "baidu/du.js", 'bg-page/option-page/js/op.js', 'bg-page/option-page/js/tab.js', "bg-page/bg.js" ]

html_to_compress = 'bg-page/option-page/main.html'


# Compress a file from path to compressPath
def compressJS( path ):
    os.system( js_compressor + ' ' + '../' + path + output_js_mark + compressPath + path )
    print( path + ' ===>> ' + compressPath + path )
def compressJSTo( path1, path2 ):
    os.system( js_compressor + ' ' + path1 + output_js_mark + path2 )
    print( path1 + ' ===>> ' + path2 )
def compressCSS( path ):
    os.system( css_compressor + ' ' + '../' + path + output_css_mark + compressPath + path )
    print( path + ' ===>> ' + compressPath + path )
def compressCSSTo( path1, path2 ):
    os.system( css_compressor + ' ' + path1 + output_css_mark + path2 )
    print( path1 + ' ===>> ' + path2 )

def compressHTML( file ):
    os.system( html_compressor + ' ../' + file + ' > ' + compressPath + file )


def joinFiles( in_files, out_file ):
    with open( out_file, 'w', encoding = 'utf-8' ) as wFileHandler:
        for file in in_files:
            with open( '../' + file, 'r', encoding = 'utf-8' ) as rFileHandler:
                wFileHandler.write( rFileHandler.read() )

def compressAll( files, type ):
    if type == 'js':
        for item in files:
            compressJS( item )
    elif type == 'css':
        for item in files:
            compressCSS( item )

def zip( item ):
    os.system( ziper + ' a -tzip ' + 'build.zip ' + item + ' ' + zip_level )
    print( item + ' has been zipped.\n' )

def getVersion():
    regExp = re.compile( '\Wversion' )
    with open( '../manifest.json', 'r', encoding = 'utf-8' ) as fileHandler:
        for line in fileHandler.readlines():
            if regExp.search( line ):
                print( 'Version info: ' + line )
                return( line )

def writeVersion( versionLine ):
    with open( compressPath + 'manifest.json', 'r', encoding = 'utf-8' ) as rFileHandler:
        fileLines = rFileHandler.readlines()
    regExp = re.compile( '\Wversion' )
    with open( compressPath + 'manifest.json', 'w', encoding = 'utf-8' ) as wFileHandler:
        for line in fileLines:
            if regExp.search( line ):
                wFileHandler.write( versionLine )
                print( 'Build version has been updated.\n' )
            else:
                wFileHandler.write( line )

def prepareFolders():
    if not os.path.exists( 'temp/' ):
        os.mkdir( 'temp/' )
def clearFolders():
    if os.path.exists( 'temp/' ):
        shutil.rmtree( 'temp/' )

def addUpdateURL():
    with open( compressPath + 'manifest.json', 'r', encoding = 'utf-8' ) as rFileHandler:
        fileLines = rFileHandler.readlines()
    del fileLines[0]
    with open( compressPath + 'manifest.json', 'w', encoding = 'utf-8' ) as wFileHandler:
        wFileHandler.write( '{ "update_url": "https://xiaody.googlecode.com/svn/crx/BDB3/utils/temp.xml",\n' )
        for line in fileLines:
            wFileHandler.write( line )

def removeUpdateURL():
    with open( compressPath + 'manifest.json', 'r', encoding = 'utf-8' ) as rFileHandler:
        fileLines = rFileHandler.readlines()
    del fileLines[0]
    with open( compressPath + 'manifest.json', 'w', encoding = 'utf-8' ) as wFileHandler:
        wFileHandler.write( '{\n' )
        for line in fileLines:
            wFileHandler.write( line )


print( '************ Build begin! ************\n' )
prepareFolders()

# update version in manifest,json
versionLine = getVersion()
writeVersion( versionLine )

# copy HTML files
compressHTML( html_to_compress )
print( '************ Option page compressed. ************\n' )

# copress css files
compressAll( css_to_compress, 'css' )
print( '************ All css files compressed. ************\n' )

# join files
joinFiles( [ "tieba/Encoder.js", "tieba/startrun.js" ], 'temp/startrun.js' )
joinFiles( [ "tieba/UUcode.js","tieba/Editor.js","tieba/PostClass.js","tieba/HotKey.js","tieba/final.js" ], 'temp/final.js' )

compressJSTo( 'temp/startrun.js', 'build/tieba/startrun.js' )
compressJSTo( 'temp/final.js', 'build/tieba/final.js' )

# copress js files
compressAll( js_to_compress, 'js' )
print( '************ All javascript files compressed. ************\n' )

# zip all files
zip( compressPath )

# make the crx package
addUpdateURL()
os.popen( crx_builder + crx_cmdlines )
print( '************ CRX build successed! ************\n' )

print( 'Build finished.\n' )
input()

clearFolders()
removeUpdateURL()
